package cn.doctorpda.meeting.okhttp.https;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.text.TextUtils;

import cn.doctorpda.meeting.MyApplication;
import cn.doctorpda.meeting.config.AppConfig;
import cn.doctorpda.meeting.localstore.ClientInfo;
import cn.doctorpda.meeting.utils.AppUtils;
import cn.doctorpda.meeting.utils.NetUtils;

/**
 * Created by huangjinlong on 2016/5/12.
 */
public class GetUrl {

    public static String appendClientInfo(String url) {
        Context context = MyApplication.getInstance();
        PackageInfo info;
        try {
            info = context.getPackageManager().getPackageInfo(context.getPackageName(), 0);
        } catch (PackageManager.NameNotFoundException e) {
            info = null;
        }
        String net;
        net =  "wifi";
        /*if (NetUtils.isWifi(context)) {
            net = "wifi";
        } else {
            net = "monet";
        }*/
        String vn = String.valueOf(AppUtils.getVersionCode(context));
        String vc = AppUtils.getVersionName(context);
        StringBuilder sb = new StringBuilder();
        sb.append(appendSuffix(url));
        sb.append("app_key=");
        sb.append(AppConfig.APP_KEY);
        sb.append("&client_id=");
        sb.append(ClientInfo.getClientId(context));
        sb.append("&net=").append(net).append("&versionName=").append(vn).append("&versionCode=").append(vc);
        String accessToken = ClientInfo.getUserAccessToken(context);
        if (!TextUtils.isEmpty(accessToken)) {
            sb.append("&access_token=");
            sb.append(accessToken);
        }
        return sb.toString();

    }
    static String appendSuffix(String url) {
        return url + getNextUrlSymbol(url);
    }
    static String getNextUrlSymbol(String url) {
        char last = url.charAt(url.length() - 1);
        String startChar = "";
        if (last != '?' && last != '&') {
            if (url.contains("?"))
                startChar = "&";
            else
                startChar = "?";
        }
        return startChar;
    }
}
